import { createSlice } from "@reduxjs/toolkit";
import Taro from "@tarojs/taro";

interface UserInfoType {
  accountType: number | null;
  merchantType: number | null;
  token: string;
  bindMerchantId: number | null;
  merchantInfo: Record<string, any>;
  offlineStores: Record<string, any>;
  shopOwnerInfo: Record<string, any>;
  openId: string;
  userId: number;
  consumerName: string;
  consumerImage: string;
}

const initialState: UserInfoType = {
  accountType: null,
  merchantType: null,
  token: "",
  bindMerchantId: null,
  merchantInfo: {},
  offlineStores: {},
  shopOwnerInfo: {},
  openId: "",
  userId: 0,
  consumerName: "",
  consumerImage: "",
};

const userInfoSlice = createSlice({
  name: "userInfo",
  initialState,
  reducers: {
    updateMerchantInfo(state, actions) {
      const {
        merchantName,
        merchantImages,
        merchantUserName,
        merchantPhone,
        merchantAddress,
        merchantLogo,
        merchantsBusinessHours,
      } = actions.payload;

      state.merchantInfo.merchantName = merchantName;
      state.merchantInfo.merchantsBusinessHours = merchantsBusinessHours;
      state.merchantInfo.merchantImages = merchantImages;
      state.offlineStores.merchantUserName = merchantUserName;
      state.offlineStores.merchantPhone = merchantPhone;
      state.offlineStores.merchantAddress = merchantAddress;
      state.offlineStores.merchantLogo = merchantLogo;

      Taro.setStorageSync("userInfo", {
        accountType: state.accountType,
        merchantType: state.merchantType,
        token: state.token,
        bindMerchantId: state.bindMerchantId,
        merchantInfo: {
          ...state.merchantInfo,
          shopOwnerInfo: state.shopOwnerInfo,
          offlineStores: state.offlineStores,
        },
      });
    },
    setUserInfo(state, action) {
      const {
        accountType,
        merchantType,
        bindMerchantId,
        merchantInfo,
        userId,
        consumerName,
        consumerImage,
        token,
      } = action.payload;
      state.accountType = accountType || null;
      state.merchantType = merchantType || null;
      state.bindMerchantId = bindMerchantId || null;
      state.merchantInfo = merchantInfo || {};
      state.offlineStores = merchantInfo?.offlineStores || {};
      state.shopOwnerInfo = merchantInfo?.shopOwnerInfo || {};
      state.userId = userId;
      state.consumerName = consumerName || "";
      state.consumerImage = consumerImage || "";
      state.token = token;
    },
    setShopOwerInfo(state, action) {
      state.shopOwnerInfo = action.payload;
      Taro.setStorageSync("userInfo", {
        accountType: state.accountType,
        merchantType: state.merchantType,
        token: state.token,
        bindMerchantId: state.bindMerchantId,
        merchantInfo: {
          ...state.merchantInfo,
          shopOwnerInfo: state.shopOwnerInfo,
          offlineStores: state.offlineStores,
        },
      });
    },
    setOpenId(state, action) {
      state.openId = action.payload;
    },
  },
});

export default userInfoSlice.reducer;
export const { setUserInfo, setShopOwerInfo, setOpenId, updateMerchantInfo } =
  userInfoSlice.actions;
